package com.tomecode.tools.maven.settings.gui;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.layout.grouplayout.GroupLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import com.tomecode.tools.maven.settings.config.Maven;
import com.tomecode.tools.maven.settings.gui.images.ImageFactory;
import com.tomecode.tools.maven.settings.gui.utils.GuiUtils;

/**
 * Dialog which contains list of maven homes and repositories
 * 
 * @author Tomas Frastia
 * @see http://www.tomecode.com or
 *      http://code.google.com/p/maven-settings-editor/
 * 
 */
public final class FrmListMavens extends Dialog {

	private List<Maven> mavens;
	private Shell shlListOfMavens;
	private Table tableMavens;
	private Button btnActivate;
	private List<Maven> result;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public FrmListMavens(Shell parent, List<Maven> mavens) {
		super(parent, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
		setText("List Of Mavens");
		this.mavens = mavens;
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public final List<Maven> open() {
		createContents();
		shlListOfMavens.open();
		shlListOfMavens.layout();
		Display display = getParent().getDisplay();
		while (!shlListOfMavens.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private final void createContents() {
		shlListOfMavens = new Shell(getParent(), getStyle());
		shlListOfMavens.addShellListener(new ShellAdapter() {
			@Override
			public final void shellActivated(ShellEvent e) {
				GuiUtils.filltableMavens(tableMavens, mavens);
			}
		});
		shlListOfMavens.setImage(ImageFactory.MAVEN_SETTINGS_EDITOR_16);
		shlListOfMavens.setSize(778, 343);
		shlListOfMavens.setText("List Of Mavens");

		Composite composite = new Composite(shlListOfMavens, SWT.NONE);
		GroupLayout gl_shlListOfMavens = new GroupLayout(shlListOfMavens);
		gl_shlListOfMavens.setHorizontalGroup(gl_shlListOfMavens.createParallelGroup(GroupLayout.LEADING).add(composite, GroupLayout.DEFAULT_SIZE, 772, Short.MAX_VALUE));
		gl_shlListOfMavens.setVerticalGroup(gl_shlListOfMavens.createParallelGroup(GroupLayout.LEADING).add(composite, GroupLayout.DEFAULT_SIZE, 318, Short.MAX_VALUE));

		Group group = new Group(composite, SWT.NONE);

		final Button btnDelete = new Button(group, SWT.NONE);
		final Button btnEdit = new Button(group, SWT.NONE);
		btnActivate = new Button(group, SWT.NONE);

		tableMavens = new Table(group, SWT.BORDER | SWT.FULL_SELECTION | SWT.V_SCROLL | SWT.H_SCROLL | SWT.CHECK);
		GuiUtils.filltableMavens(tableMavens, mavens);
		tableMavens.addMouseListener(new MouseAdapter() {
			@Override
			public final void mouseDoubleClick(MouseEvent e) {
				int index = tableMavens.getSelectionIndex();
				if (index == -1) {
					btnEdit.setEnabled(false);
					btnDelete.setEnabled(false);
				} else {
					openFrmMavenForEdit(index);
					btnEdit.setEnabled(true);
					btnDelete.setEnabled(true);
				}
				checkButtonActive(index);
			}
		});
		tableMavens.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				int index = tableMavens.getSelectionIndex();
				btnEdit.setEnabled(index != -1);
				btnDelete.setEnabled(index != -1);
				checkButtonActive(index);
			}
		});
		tableMavens.setHeaderVisible(true);
		tableMavens.setLinesVisible(true);

		TableColumn tblclmnVersion = new TableColumn(tableMavens, SWT.CENTER);
		tblclmnVersion.setMoveable(true);
		tblclmnVersion.setWidth(56);
		tblclmnVersion.setText("Version");

		TableColumn tblclmnMavenHome = new TableColumn(tableMavens, SWT.CENTER);
		tblclmnMavenHome.setMoveable(true);
		tblclmnMavenHome.setWidth(331);
		tblclmnMavenHome.setText("Maven Home");

		TableColumn tblclmnMavenRepo = new TableColumn(tableMavens, SWT.CENTER);
		tblclmnMavenRepo.setMoveable(true);
		tblclmnMavenRepo.setWidth(331);
		tblclmnMavenRepo.setText("Maven Repo");

		Button btnDeleteAll = new Button(group, SWT.NONE);
		btnDeleteAll.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				mavens.clear();
				GuiUtils.filltableMavens(tableMavens, mavens);
			}
		});
		btnDeleteAll.setText("Delete All");

		btnDelete.setEnabled(false);
		btnDelete.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				mavens.remove(tableMavens.getSelectionIndex());
				GuiUtils.filltableMavens(tableMavens, mavens);
			}
		});
		btnDelete.setText("Delete");

		btnEdit.setEnabled(false);
		btnEdit.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				openFrmMavenForEdit(tableMavens.getSelectionIndex());
			}
		});
		btnEdit.setText("Edit");

		Button btnAdd = new Button(group, SWT.NONE);
		btnAdd.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				FrmMaven frmMaven = new FrmMaven(shlListOfMavens);
				Maven maven = frmMaven.open(null);
				if (maven != null && maven.getHome() != null) {
					mavens.add(maven);
					GuiUtils.filltableMavens(tableMavens, mavens);
				}
			}
		});
		btnAdd.setText("Add");

		btnActivate.setEnabled(false);
		btnActivate.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				int index = tableMavens.getSelectionIndex();
				for (int i = 0; i <= mavens.size() - 1; i++) {
					Maven maven = mavens.get(i);
					if (i == index) {
						maven.setActive(!maven.isActive());
					} else {
						maven.setActive(false);
					}
				}
				GuiUtils.filltableMavens(tableMavens, mavens);

			}
		});
		btnActivate.setText("Activate");

		Button btnSave = new Button(composite, SWT.NONE);
		btnSave.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				result = new ArrayList<Maven>();
				for (TableItem item : tableMavens.getItems()) {
					Maven maven = new Maven();
					maven.setVersion(item.getText(0));
					maven.setActive(item.getChecked());
					maven.setHome(new File(item.getText(1)));
					maven.setRepository(new File(item.getText(2)));
					result.add(maven);
				}

				shlListOfMavens.close();
			}
		});
		btnSave.setText("Save");

		Button btnCancel = new Button(composite, SWT.NONE);
		btnCancel.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				result = null;
				shlListOfMavens.close();
			}
		});
		btnCancel.setText("Cancel");
		GroupLayout gl_composite = new GroupLayout(composite);
		gl_composite.setHorizontalGroup(gl_composite.createParallelGroup(GroupLayout.LEADING).add(
				gl_composite
						.createSequentialGroup()
						.add(gl_composite.createParallelGroup(GroupLayout.LEADING)
								.add(gl_composite.createSequentialGroup().add(568).add(btnSave, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE).add(6).add(btnCancel, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE))
								.add(group, GroupLayout.DEFAULT_SIZE, 771, Short.MAX_VALUE)).add(1)));
		gl_composite.setVerticalGroup(gl_composite.createParallelGroup(GroupLayout.LEADING).add(
				gl_composite.createSequentialGroup().add(group, GroupLayout.PREFERRED_SIZE, 258, GroupLayout.PREFERRED_SIZE).add(20).add(gl_composite.createParallelGroup(GroupLayout.LEADING).add(btnSave).add(btnCancel))));
		GroupLayout gl_group = new GroupLayout(group);
		gl_group.setHorizontalGroup(gl_group
				.createParallelGroup(GroupLayout.LEADING)
				.add(tableMavens, GroupLayout.PREFERRED_SIZE, 762, GroupLayout.PREFERRED_SIZE)
				.add(gl_group.createSequentialGroup().add(271).add(btnActivate, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE).add(6).add(btnAdd, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE).add(6)
						.add(btnEdit, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE).add(6).add(btnDelete, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE).add(6).add(btnDeleteAll, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE)));
		gl_group.setVerticalGroup(gl_group.createParallelGroup(GroupLayout.LEADING).add(
				gl_group.createSequentialGroup().add(tableMavens, GroupLayout.PREFERRED_SIZE, 211, GroupLayout.PREFERRED_SIZE).add(6).add(gl_group.createParallelGroup(GroupLayout.LEADING).add(btnActivate).add(btnAdd).add(btnEdit).add(btnDelete).add(btnDeleteAll))));
		group.setLayout(gl_group);
		composite.setLayout(gl_composite);
		shlListOfMavens.setLayout(gl_shlListOfMavens);

	}

	private final void checkButtonActive(int index) {
		if (index == -1) {
			btnActivate.setEnabled(false);
		} else {
			btnActivate.setEnabled(true);
			if (mavens.get(index).isActive()) {
				btnActivate.setText("Deactive");
			} else {
				btnActivate.setText("Active");
			}
		}
	}

	/**
	 * open dialog {@link FrmMaven} 
	 * 
	 * @param index
	 */
	private final void openFrmMavenForEdit(int index) {
		FrmMaven frmMaven = new FrmMaven(shlListOfMavens);
		Maven maven = frmMaven.open(mavens.get(index));
		if (maven != null && maven.getHome() != null) {
			mavens.set(index, maven);
			GuiUtils.filltableMavens(tableMavens, mavens);
		}
	}
}
